package com.yulintu.utils;

import com.yulintu.common.FileBodyResult;
import com.yulintu.header.Headr;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析文件
 * @author caomin
 * @date 2017/10/27
 * @说明
 */
@Component
public class FileParser implements Runnable{


    @Autowired
    FileParserUtils fileParserUtils;

    private   File file=null;

    /**
     * 包头
     */
    private   List<Headr>headrs=new ArrayList<>();

    /**
     * body体，包含head与内容
     */
    private  List<FileBodyResult> fileBodyResults=new ArrayList<>();



    /**
     * 解析文件，将文件头和body分开
     */
    public void parserFile() throws IOException {
        if (file==null)
            return;
        LineIterator it = FileUtils.lineIterator(file, "UTF-8");
        FileBodyResult fileBodyResult=null;
        StringBuffer bodyData = new StringBuffer();
        //标识开始解析body体
        boolean body=false;
        //标识开始解析body中的hear体
        boolean bodyHeader=false;
        try {
            while (it.hasNext()) {
                String line = it.nextLine();
                /**
                 * 解析包头
                 */
                if (line.startsWith("package-")) {
                    int beginIndex = line.indexOf(":");
                    String headerKey = line.substring(0, beginIndex);
                    String headerValue = line.substring(beginIndex, line.length());
                    this.getHeadrs().add(new Headr(headerKey,headerValue));
                } else if (line.startsWith("Content-Type")){
                    fileBodyResult=new FileBodyResult();
                    bodyHeader=true;
                    body=false;
                }
                else if (line.isEmpty()){
                    if (body){
                        fileBodyResult.setData(bodyData.toString());
                        bodyData.delete(0,bodyData.length());
                        fileBodyResults.add(fileBodyResult);
                    }
                    else if (bodyHeader){
                        body=true;
                        bodyHeader=false;
                    }else {
                        body=false;
                    }
                }
                if (bodyHeader){
                    int beginIndex = line.indexOf(":");
                    String headerKey = line.substring(0, beginIndex);
                    String headerValue = line.substring(beginIndex+1, line.length());
                    fileBodyResult.getHeadrs().add(new Headr(headerKey,headerValue));
                }
                if (body){
                    bodyData.append(line);
                }
            }
            String s = bodyData.toString();
            if (!s.isEmpty()){
                fileBodyResult.setData(s);
                fileBodyResults.add(fileBodyResult);
            }
        } finally {
            System.out.println("文件解析结束");
            LineIterator.closeQuietly(it);
        }
    }
    public List<Headr> getHeadrs() {
        return headrs;
    }

    public void setHeadrs(List<Headr> headrs) {
        this.headrs = headrs;
    }


    public FileParser setFile(File file) {
        this.file = file;return this;
    }

    public File getFile() {
        return file;
    }

    public List<FileBodyResult> getFileBodyResults() {
        return fileBodyResults;
    }

    public void setFileBodyResults(List<FileBodyResult> fileBodyResults) {
        this.fileBodyResults = fileBodyResults;
    }
    public FileParser(File file){
        this.file=file;
    }

    public FileParser() {
    }


    @Override
    public void run() {
        try {
            System.out.println("开始解析文件=====》");
            this.parserFile();
            try {
                fileParserUtils.paerser(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
